This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add image orientation option #45
Open
ghost
wants to merge
1
commit into
google:master
Choose a base branch
from
unknown repository
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
demo/src/main/java/com/google/android/cameraview/demo/CompressImageCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.google.android.cameraview.demo; | ||
|
||
/** | ||
* | ||
* Compress image async task callback | ||
*/ | ||
public interface CompressImageCallback { | ||
void onImageCompressed(byte[] image); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
demo/src/main/java/com/google/android/cameraview/demo/RotateImageTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.google.android.cameraview.demo; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Matrix; | ||
import android.os.AsyncTask; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
/** | ||
* Created by damian on 09/11/2016. | ||
* | ||
* Compressing and rotating byte array | ||
*/ | ||
class RotateImageTask extends AsyncTask<byte[], Void, byte[]> { | ||
|
||
private CompressImageCallback mListener; | ||
private int angle; | ||
|
||
public RotateImageTask(CompressImageCallback mListener, int angle) { | ||
this.mListener = mListener; | ||
this.angle = angle; | ||
} | ||
|
||
@Override | ||
protected byte[] doInBackground(byte[]... params) { | ||
byte[] data = params[0]; | ||
Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length); | ||
Bitmap bmp = rotateBitmap(image, angle); | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); | ||
return stream.toByteArray(); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(byte[] bitmap) { | ||
super.onPostExecute(bitmap); | ||
mListener.onImageCompressed(bitmap); | ||
} | ||
|
||
private Bitmap rotateBitmap(Bitmap source, int angle) { | ||
Matrix matrix = new Matrix(); | ||
matrix.postRotate(angle); | ||
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FFFFFF" | ||
android:pathData="M7.11,8.53L5.7,7.11C4.8,8.27 4.24,9.61 4.07,11h2.02c0.14,-0.87 0.49,-1.72 1.02,-2.47zM6.09,13L4.07,13c0.17,1.39 0.72,2.73 1.62,3.89l1.41,-1.42c-0.52,-0.75 -0.87,-1.59 -1.01,-2.47zM7.1,18.32c1.16,0.9 2.51,1.44 3.9,1.61L11,17.9c-0.87,-0.15 -1.71,-0.49 -2.46,-1.03L7.1,18.32zM13,4.07L13,1L8.45,5.55 13,10L13,6.09c2.84,0.48 5,2.94 5,5.91s-2.16,5.43 -5,5.91v2.02c3.95,-0.49 7,-3.85 7,-7.93s-3.05,-7.44 -7,-7.93z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
library/src/main/base/com/google/android/cameraview/ExifUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.google.android.cameraview; | ||
|
||
import android.util.Log; | ||
|
||
/** | ||
* byte array orientation util | ||
*/ | ||
public abstract class ExifUtil { | ||
private static final String TAG = "CameraExif"; | ||
|
||
/** | ||
* Computing image orientation | ||
* | ||
* @param jpeg byte array from camera | ||
* @return the degrees in clockwise. Values are 0, 90, 180, or 270. Returns 0 if undefined or rotation not needed | ||
*/ | ||
public static int getOrientation(byte[] jpeg) { | ||
if (jpeg == null) { | ||
return 0; | ||
} | ||
|
||
int offset = 0; | ||
int length = 0; | ||
|
||
// ISO/IEC 10918-1:1993(E) | ||
while (offset + 3 < jpeg.length && (jpeg[offset++] & 0xFF) == 0xFF) { | ||
int marker = jpeg[offset] & 0xFF; | ||
|
||
// Check if the marker is a padding. | ||
if (marker == 0xFF) { | ||
continue; | ||
} | ||
offset++; | ||
|
||
// Check if the marker is SOI or TEM. | ||
if (marker == 0xD8 || marker == 0x01) { | ||
continue; | ||
} | ||
// Check if the marker is EOI or SOS. | ||
if (marker == 0xD9 || marker == 0xDA) { | ||
break; | ||
} | ||
|
||
// Get the length and check if it is reasonable. | ||
length = pack(jpeg, offset, 2, false); | ||
if (length < 2 || offset + length > jpeg.length) { | ||
Log.e(TAG, "Invalid length"); | ||
return 0; | ||
} | ||
|
||
// Break if the marker is EXIF in APP1. | ||
if (marker == 0xE1 && length >= 8 && | ||
pack(jpeg, offset + 2, 4, false) == 0x45786966 && | ||
pack(jpeg, offset + 6, 2, false) == 0) { | ||
offset += 8; | ||
length -= 8; | ||
break; | ||
} | ||
|
||
// Skip other markers. | ||
offset += length; | ||
length = 0; | ||
} | ||
|
||
// JEITA CP-3451 Exif Version 2.2 | ||
if (length > 8) { | ||
// Identify the byte order. | ||
int tag = pack(jpeg, offset, 4, false); | ||
if (tag != 0x49492A00 && tag != 0x4D4D002A) { | ||
Log.e(TAG, "Invalid byte order"); | ||
return 0; | ||
} | ||
boolean littleEndian = (tag == 0x49492A00); | ||
|
||
// Get the offset and check if it is reasonable. | ||
int count = pack(jpeg, offset + 4, 4, littleEndian) + 2; | ||
if (count < 10 || count > length) { | ||
Log.e(TAG, "Invalid offset"); | ||
return 0; | ||
} | ||
offset += count; | ||
length -= count; | ||
|
||
// Get the count and go through all the elements. | ||
count = pack(jpeg, offset - 2, 2, littleEndian); | ||
while (count-- > 0 && length >= 12) { | ||
// Get the tag and check if it is orientation. | ||
tag = pack(jpeg, offset, 2, littleEndian); | ||
if (tag == 0x0112) { | ||
// We do not really care about type and count, do we? | ||
int orientation = pack(jpeg, offset + 8, 2, littleEndian); | ||
switch (orientation) { | ||
case 1: | ||
return 0; | ||
case 3: | ||
return 180; | ||
case 6: | ||
return 90; | ||
case 8: | ||
return 270; | ||
} | ||
return 0; | ||
} | ||
offset += 12; | ||
length -= 12; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private static int pack(byte[] bytes, int offset, int length, | ||
boolean littleEndian) { | ||
int step = 1; | ||
if (littleEndian) { | ||
offset += length - 1; | ||
step = -1; | ||
} | ||
|
||
int value = 0; | ||
while (length-- > 0) { | ||
value = (value << 8) | (bytes[offset] & 0xFF); | ||
offset += step; | ||
} | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be
out.writeByte((byte) (computeAngle ? 1 : 0));
?