gpt4 book ai didi

Android:拍照,然后立即进行裁剪而无需从图库中选择

转载 作者:行者123 更新时间:2023-11-30 03:04:24 29 4
gpt4 key购买 nike

我有完整的工作代码来拍照、裁剪、发送带有图像附件的电子邮件。

代码流程如下:拍张照片 > 找到并选择我们刚刚从图库中拍摄的照片> 裁剪它 > 通过电子邮件发送附件。

我想了解如何跳过整个“查找并选择我们刚刚从画廊拍摄的照片”部分,并在拍完照片后立即直接进行裁剪。 p>

是否有一个 Intent 可以满足我的要求?

我试过这个教程,但它仍然会让用户进入画廊并寻找他们刚刚拍摄的照片 http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/

编辑 我还应该提到我在拍照时有一个自定义 View ,所以我不确定我是否可以使用 Intent MediaStore.ACTION_IMAGE_CAPTURE

这是执行裁剪的代码,还可以让您通过查看图库来选择刚刚拍摄的图像。

        Intent crop_pic = new Intent(Intent.ACTION_PICK, uriTarget); // used to be ACTION_GET_CONTENT /ACTION_PICK
crop_pic.putExtra("crop", "true"); // this enables crop feature
//crop_pic.putExtra("aspectX", 200); // this defines the aspect ration
//crop_pic.putExtra("aspectY", 150);
//crop_pic.putExtra("outputX", 500); // this defines the output bitmap size
//crop_pic.putExtra("outputY", 500);
//crop_pic.putExtra("scale", true);
try { crop_pic.putExtra("return-data", false); // true to return a Bitmap, false to directly save the cropped iamge
crop_pic.putExtra(MediaStore.EXTRA_OUTPUT, uriTarget); // save output image in uri
//crop_pic.setDataAndType(uriTarget, "image/*"); // this will open all images in the Galery
startActivityForResult(crop_pic, EMAIL_PIC);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
catch (ActivityNotFoundException e) {
toast.showToastDialog(getApplicationContext(), e.toString());
}
LOGCAT.DEBUG(TAG, "Cropping Picture");

}

如果我为它提供一个 uri 目标,是否有一个我不知道的 Intent 可以开始裁剪?

完整代码如下:

 public class DigitalSignature extends Activity implements SurfaceHolder.Callback{

Camera camera;
SurfaceHolder surfaceHolder;
boolean previewing = false;

Uri uriTarget;
String[] recipients = new String[]{"test@gmail.com", "",};
final int CROP_PIC = 1, EMAIL_PIC = 2, RETURN_ACTIVITY = 3, FINISH = 4, INDEX_GALLERY = 5;

final String TAG = "Digital Signature";

ToastBuilder toast = new ToastBuilder(this);
ProjectDebug LOGCAT = new ProjectDebug();
ProgressDialogManager pDialog = new ProgressDialogManager();




@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

DigitalSignature.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
DigitalSignature.this.setContentView(R.layout.digitalsignature);
DigitalSignature.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

DigitalSignature.this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DigitalSignature.this.getWindow().setFormat(PixelFormat.TRANSLUCENT); // Used to be TRANSPARENT



SurfaceView surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(DigitalSignature.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

LayoutInflater controlInflater = LayoutInflater.from(getApplicationContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

DigitalSignature.this.addContentView(viewControl, layoutParamsControl); // Applying Filters and Parameters


TextView alignsig = (TextView)findViewById(R.id.alignsig);
alignsig.setText("Please take picture of the signature for" + "\n" + "Job Number " + DIGIJOB);

Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){



@Override
public void onClick(View arg0) {
LOGCAT.DEBUG(TAG, "Taking Picture");


// Sets camera monochrome filter ( Black and Whiite )
Camera.Parameters parameters = camera.getParameters();
parameters.setColorEffect(android.hardware.Camera.Parameters.EFFECT_MONO);
camera.setParameters(parameters);
camera.startPreview();


// Initiate Camera Focus
camera.autoFocus(mAutoFocusCallback);

// Creates Directory to save image in gallery
uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

//Notify the MediaScanner that a new file has been added and should be indexed so it shows up in the MediaStore.
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(uriTarget);
sendBroadcast(intent);

}
});
}






Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {

@Override
public void onAutoFocus(boolean success, Camera camera) {

camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
}
};


ShutterCallback myShutterCallback = new ShutterCallback(){

@Override
public void onShutter() {

}};


PictureCallback myPictureCallback_RAW = new PictureCallback(){

@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {

}};


PictureCallback myPictureCallback_JPG = new PictureCallback(){

@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
//bmp = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

cropPic();

OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();

toast.showToastDialog(getApplicationContext(), "Image saved: " + uriTarget.toString());
}


catch (IOException e) {
toast.showToastDialog(getApplicationContext(), e.toString());
}

// camera.startPreview(); // No need to start preview, we are saving
}}; // picture and no longer initiating camera






protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case CROP_PIC:
cropPic();
LOGCAT.DEBUG(TAG, "cropPic Case");
break;


case EMAIL_PIC:
emailPic();
LOGCAT.DEBUG(TAG, "emailPic Case");
break;


case RETURN_ACTIVITY:
returnActivity();
LOGCAT.DEBUG(TAG, "returnActivity Case");
break;
}
}



private void cropPic() {


Intent crop_pic = new Intent(Intent.ACTION_PICK, uriTarget); // used to be ACTION_GET_CONTENT /ACTION_PICK
crop_pic.putExtra("crop", "true"); // this enables crop feature
//crop_pic.putExtra("aspectX", 200); // this defines the aspect ration
//crop_pic.putExtra("aspectY", 150);
//crop_pic.putExtra("outputX", 500); // this defines the output bitmap size
//crop_pic.putExtra("outputY", 500);
//crop_pic.putExtra("scale", true);
try { crop_pic.putExtra("return-data", false); // true to return a Bitmap, false to directly save the cropped iamge
crop_pic.putExtra(MediaStore.EXTRA_OUTPUT, uriTarget); // save output image in uri
//crop_pic.setDataAndType(uriTarget, "image/*"); // this will open all images in the Galery
startActivityForResult(crop_pic, EMAIL_PIC);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
catch (ActivityNotFoundException e) {
toast.showToastDialog(getApplicationContext(), e.toString());
}
LOGCAT.DEBUG(TAG, "Cropping Picture");

}



private void emailPic(){

// new email_pic().execute();

GMailSender m = new GMailSender("username@gmail.com", "testtest");

String[] toArr = { "sendingto@gmail.com" };
m.setTo(toArr);
m.setFrom("sendingto@gmail.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");

try {
m.addAttachment(getRealPathFromURI(uriTarget));

if(m.send()) {
Toast.makeText(DigitalSignature.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(DigitalSignature.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}

} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}


// OLD METHOD OF SENDING EMAIL
/* Intent email_pic = new Intent(Intent.ACTION_SEND); // Commence choose photo action
email_pic.putExtra(Intent.EXTRA_EMAIL, recipients); // Inserts Digital Signature address automatically in recipient
email_pic.putExtra(Intent.EXTRA_SUBJECT, DIGIJOB); // Inserts Track Number automatically in subject
email_pic.putExtra(Intent.EXTRA_STREAM, uriTarget); // Attaches image to Gmail
email_pic.setType("image/jpeg"); // Converts image to jpeg
startActivityForResult(email_pic, RETURN_ACTIVITY);
overridePendingTransition(R.anim.fadein, R.anim.fadeout); */
JOB = "job"; // Reset job variable from track no (1736) to "job" so
LOGCAT.DEBUG(TAG, "Emailing Picture"); // when you go back into Jobs Activity, it can parse data.

returnActivity();

}






private void returnActivity() {
Intent email_pic = new Intent(DigitalSignature.this, AgentPortalActivity.class);
startActivityForResult(email_pic, FINISH);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
LOGCAT.DEBUG(TAG, "Returning Activity");
}





private String getRealPathFromURI(Uri uriTarget) {

String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uriTarget, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

LOGCAT.DEBUG("getRealPathFromURI", cursor.getString(column_index));
return cursor.getString(column_index);

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {


if (previewing){
camera.stopPreview();
previewing = false;
}

if (camera != null){

try { camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}

catch (IOException e) {
toast.showToastDialog(getApplicationContext(), e.toString());
}
}
}




@Override
public void surfaceCreated(SurfaceHolder holder) {

camera = Camera.open();
//camera = Camera.open(0);

}




@Override
public void surfaceDestroyed(SurfaceHolder holder) {

camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}

最佳答案

您只需要使用 MediaStore.ACTION_IMAGE_CAPTURE Intent 操作。这将启动相机。在您的 onActivityResult 中,您可以获得刚刚拍摄的图像。现在您可以裁剪并发送它以及您想要的任何其他内容。下面是一个例子。

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File pics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File cameraPhotoFile = new File(pics, System.currentTimeMillis()+".jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraPhotoFile));
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) return;
if (requestCode == REQUEST_CODE_CAMERA) {
MediaScannerConnection.scanFile(this,
new String[]{cameraPhotoFile.getAbsolutePath()}, null, null);

// do something with the image located at cameraPhotoFile

我已经包含了 MediaScannerConnection,因为图像在扫描之前不会显示在图库中。如果你愿意,你可以删除它,但我喜欢拍摄的图像显示出来。位图现在位于“cameraPhotoFile”中。随心所欲地使用该文件。

另外,这里是来自谷歌的关于这个叫做 Taking Photos Simply 的文档。 .这很简单

关于Android:拍照,然后立即进行裁剪而无需从图库中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056891/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com