我想在纵向模式下在Surfaceview中设置我的捕获图像,所以下面是我的代码
保存捕获的图像
private String imgPathHacked = "";
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
long time = 0;
try {
time = System.currentTimeMillis();
FileUtility fileUtility = new FileUtility();
File file = new File(fileUtility.getDirectoryName(NumericPassActivity.this)+Constans.HACK_ATTEMPT_URI);
if(!file.exists()){
file.mkdirs();
}
File dataFile = new File(file.getAbsolutePath(),String.format("%d.jpg", System.currentTimeMillis()));
if(!dataFile.exists()){
dataFile.createNewFile();
}
Uri uri = Uri.fromFile(dataFile.getAbsoluteFile());
camera.setDisplayOrientation(getCameraPhotoOrientation(NumericPassActivity.this, uri , dataFile.getAbsolutePath()));
outStream = new FileOutputStream(dataFile);
outStream.write(data);
outStream.close();
imgPathHacked = dataFile.getAbsolutePath();
Utility utility = new Utility();
HackAttemptModel model = new HackAttemptModel();
model.setmAttemptedFor("Wrong PIN");
model.setmAttemptedPass(pass);
model.setmHackedImg(imgPathHacked);
utility.saveAttempt(model);
if(preview!=null && preview.camera!=null){
preview.camera.startPreview();
fr.removeAllViews();
fr.addView(preview);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
};
Preview.java
package com.Zebracom.filesecurity.utility;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
public Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
try {
parm.setPictureSize(150, 150);
parm.setPreviewSize(150, 150);
parm.set("orientation", "portrait");
camera.setDisplayOrientation(90);
camera.setParameters(parm);*/
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size previewSize = previewSizes.get(o);
parameters.setPreviewSize(previewSize.width, previewSize.height);
//parameters.setRotation(90);
//parameters.set("orientation", "portrait");
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
/*File file = new File(Environment.getExternalStorageDirectory()+"/camera/");
if(!file.exists()){
file.mkdirs();
}
File dataFile = new File(file.getAbsolutePath(),String.format("%d.jpg", System.currentTimeMillis()));
if(!dataFile.exists()){
dataFile.createNewFile();
}
outStream = new FileOutputStream(dataFile);
outStream.write(data);
outStream.close();*/
Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
Log.e(TAG, "surfaceCreated >> "+e.toString());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
this.getHolder().removeCallback(this);
camera.stopPreview();
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.startPreview();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p= new Paint(Color.RED);
Log.d(TAG,"draw");
canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
}
}
当我运行上面的代码时,我捕获的图像总是设置为横向模式,但我希望它在肖像中,所以任何想法我怎么能解决这个问题?你的所有建议都很明显。
答案 0 :(得分:1)
你为什么不使用这样的东西?
要调用相机意图,请使用此按钮单击:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
((Activity) context).startActivityForResult(intent, 1);
并且在画布使用此方法的活动中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final ImageView uploadArea = (ImageView) attachmentDialog.findViewById(R.id.uploadArea);
final EditText attachmentTitle = (EditText) attachmentDialog.findViewById(R.id.txtEditTitleAttach);
Bitmap bitmap;
if (resultCode == RESULT_OK) {
if (requestCode == Constants.REQUEST_IMAGE_CAPTURE) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
Matrix matrix = new Matrix();
matrix.postRotate(-90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] attachmentBytes = byteArrayOutputStream.toByteArray();
attachmentData = Base64.encodeToString(attachmentBytes, Base64.DEFAULT);
uploadArea.setImageBitmap(rotatedBitmap);
attachmentTitle.setText("temp.jpeg");
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "CTSTemp" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我希望这会有所帮助: 在上面的代码中,我使用以下3行旋转捕获的图像:
Matrix matrix = new Matrix();
matrix.postRotate(-90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);