I am using a vertical screen preview of the camera, but the preview image 90 degrees reversed, but the recorded picture is normal 90 degrees reversed.
If you use a horizontal screen preview, image preview normal, but to shoot pictures with a preview of 90 degrees reversed.
Now I would like in the case of vertical screen can preview and capture are normal, what to do?
Here is the code:
Button takebtn;
Button savebtn;
Button cancelbtn;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
File picture;
String url;
Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
Window window = getWindow ();
window.setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView (R.layout.activity_camera);
initWigdt ();
}
protected void initWigdt () {
takebtn = (Button) findViewById (R.id.takebtn);
savebtn = (Button) findViewById (R.id.savebtn);
cancelbtn = (Button) findViewById (R.id.cancelbtn);
surfaceView = (SurfaceView) findViewById (R.id.surview);
surfaceHolder = surfaceView.getHolder ();
surfaceHolder.addCallback (surfaceCallback);
surfaceHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
takebtn.setOnClickListener (new OnClickListener () {
Override
public void onClick (View v) {
// TODO Auto-generated method stub
takePic ();
}
});
}
// Save the image
private void takePic () {
camera.takePicture (null, null, pictureCallback);
}
// Display pictures
Camera.PictureCallback pictureCallback = new Camera.PictureCallback () {
public void onPictureTaken (byte [] data, Camera camera) {
new SavePictureTask () execute (data);.
camera.startPreview ();
}
};
// Save to the phone card
class SavePictureTask extends AsyncTask & lt; byte [], String, String & gt; {
Override
protected String doInBackground (byte [] ... params) {
String name = String.valueOf (new Date () getTime ().);
url = "/ sdcard /" + name + ".jpg";
File picture = new File (url);
try {
FileOutputStream fos = new FileOutputStream (picture.getPath ());
fos.write (params [0]);
fos.close ();
} Catch (Exception e) {
e.printStackTrace ();
Log.e ("save", e.getMessage ());
}
System.out.println ("Photo Save complete");
Intent aintent = new Intent (CameraActivity.this, MainActivity.class);
aintent.putExtra ("url", url);
setResult (1, aintent);
CameraActivity.this.finish ();
return null;
}
}
protected void setDisplayOrientation (Camera camera, int angle) {
Method downPolymorphic;
try {
downPolymorphic = camera.getClass (). getMethod (
"SetDisplayOrientation", new Class [] {int.class});
if (downPolymorphic! = null)
downPolymorphic.invoke (camera, new Object [] {angle});
} Catch (Exception e1) {
e1.printStackTrace ();
}
}
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback () {
public void surfaceCreated (SurfaceHolder holder) {
camera = Camera.open ();
System.out.println ("camera open complete");
try {
camera.setPreviewDisplay (holder);
} Catch (IOException e) {
camera.release ();
camera = null;
}
}
public void surfaceChanged (SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters parameters = camera.getParameters ();
parameters.setPictureFormat (PixelFormat.JPEG);
if (Integer.parseInt (Build.VERSION.SDK) & gt; = 8) {// the system determines whether the version greater than or equal
2.2setDisplayOrientation (camera, 90); // rotate 90, provided that the current page portrait, longitudinal
Log.e ("a", "cc");
} Else {// system version 2.2 or less in the following manner using the rotation
if (getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT) {
parameters.set ("orientation", "portrait");
parameters.set ("rotation", 90);
}
if (getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.set ("orientation", "landscape");
parameters.set ("rotation", 90);
}
Log.e ("c", "dd");
}
camera.setDisplayOrientation (0);
camera.setParameters (parameters);
camera.startPreview ();
}
public void surfaceDestroyed (SurfaceHolder holder) {
camera.stopPreview ();
camera.release ();
camera = null;
}
};
Reply:
This problem is very troublesome ah, the Internet has a lot in this regard, the landlord did not read?
Usually difficult to find a common approach to adapting many mobile phone on the market right now. This is the root camera hardware related
Reply:
Hardware requirements are mainly it
Reply:
http://asysbang.com/forum.php?mod=viewthread&tid=58&extra=page%3D1
Reply:
Well, to find a lot of ways, I have a rotation in the above code, but did not play a role.
Reply:
This useful. . The next test, the results of success. Thank you!
Reply:
On this site or not, the landlord can not put a specific method posted ah?
Reply:
I posted the solution, we look
1, the display
correct preview
The main reference system camera code getDisplayOritation it
// Call setDisplayOrientation
before previewint degrees = getDisplayOritation (getDispalyRotation (), cameraId);
mCamera.setDisplayOrientation (degrees);
mCamera.startPreview ();
getDisplayOritation function is as follows:
private int getDisplayOritation (int degrees, int cameraId) {
Camera.CameraInfo info = new Camera.CameraInfo ();
Camera.getCameraInfo (cameraId, info);
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees)% 360;
result = (360 - result)% 360;
} Else {
result = (info.orientation - degrees + 360)% 360;
}
return result;
}
private int getDispalyRotation () {
int i = getWindowManager () getDefaultDisplay () getRotation ();..
switch (i) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}
2, the display
correctly display pictures
Portrait photograph taken directly used, rotate
90 degrees
Reference system library code, you need to query field mediascanner orientation, and then apply the rotation angle over, so show the ok
Reference code as follows:
Suppose cursor
c is returned by the database query mediaprovider
int rotation = c.getInt (c.getColumnIndex (MediaStore.Images.ImageColumns.ORIENTATION));
if (rotation! = 0) {
Bitmap bitmap = BitmapFactory.decodeFile (path);
imageBefore.setImageBitmap (bitmap);
Matrix m = new Matrix ();
m.setRotate (rotation);
Bitmap transformed = Bitmap.createBitmap (bitmap, 0, 0, bitmap.getWidth (), bitmap.getHeight (), m, true);
return transformed;
}
Reply:
"Assuming c is returned by the database query mediaprovider cursor"
C how you get out of this?
Reply:
Thank landlord to solve my problem.
Reply:
The landlord of ideas has helped me, I have solved the problem, but according to the angle of the cursor query media library is not comprehensive, can not solve individual problems, such as calling system cameras generate big picture through the media library uri, use this method every time It will return 0, but actually reversed 90 degrees. According to my investigation, because the uri media library (format "content: ....") is not a picture of the physical path, it is a picture in the media database query address, so if generating big picture when the media library If you do not set the rotation angle values are initialized (I guess because I would not have generated bigger insert any other metadata), the default is 0, and the actual default angle is set according to the hardware, the Tablet PC is a horizontal screen mostly feeling ( 90 In order to reverse the default horizontal screen adaptation).
The solution is to eradicate the media library uri (such as "content: .... / 47.") Get Picture of physical address (such as "/xx/xx/xx1545515.jpg"), and then to get the picture by EXIFInterface according to the physical address The actual angle, according to the angle to get to use Maxtri positive. (If you use a media library uri.getpath () Gets the angle returned is 0)
OK I do not know if we do not sort out ideas
Reply:
cameraId, where they come from
No comments:
Post a Comment